<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
use kartik\date\DatePicker;

?>

<div class="invoice-form">

   <?php $form = ActiveForm::begin([
             'id' => 'dynamic-form',
             'options' => ['enctype' => 'multipart/form-data',],
             'layout' => 'horizontal',
             'fieldConfig' => [
                'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                'horizontalCssClasses' => [
                    'label' => 'col-sm-2',
                    'wrapper' => 'col-sm-7',
                    'error' => '',
                ],
             ],
        ]); ?>

    <?= Html::activeHiddenInput($model, "product_id");?>
    
    <?= $form->field($model, 'pay_type')->inline(true)->radioList(Yii::$app->params['invoice_pay_type']) ?>

    <div class="col-md-12">
        
        <div class="col-md-12" style="padding: 10px;border: 1px solid #ddd;margin: 10px;border-radius: 4px;">

        <?php DynamicFormWidget::begin([
            'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric 
            'widgetBody' => '.container-items', // required: css class selector
            'widgetItem' => '.item', // required: css class
            'limit' => 999, // the maximum times, an element can be cloned (default 999)
            'min' => 1, // 0 or 1 (default 1)
            'insertButton' => '.add-item', // css class
            'deleteButton' => '.remove-item', // css class
            'model' => $detail[0],
            'formId' => 'dynamic-form',
            'formFields' => [
                'num',
                's_price',
                'remark',
            ],
        ]); ?>

        <div class="container-items">
        <?php foreach ($detail as $index => $mem): ?>
                <div class="item">
                    <?php
                        if (!$mem->isNewRecord) {
                            echo Html::activeHiddenInput($mem, "[{$index}]id");
                        }
                    ?>
                    <div class="row">
                        <div class="col-md-4">
                        <?= $form->field($mem, "[{$index}]num")->textInput(['maxlength' => true])?>
                        </div> 
                        <div class="col-md-4">
                        <?= $form->field($mem, "[{$index}]s_price")->textInput(['maxlength' => true]);?>
                        </div> 

                        <div class="col-md-3">
                        <?= $form->field($mem, "[{$index}]remark")->textArea(['maxlength' => true]);?>
                        </div> 
                        <div class="col-md-1">
                            <button type="button" class="remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
                        </div> 
                    </div>       
                </div>
        <?php endforeach; ?>
        </div>
        <div class="pull-right">
            <button class="btn btn-primary add-item">添加</button>
        </div>       
        <?php DynamicFormWidget::end(); ?>
        </div>
    </div>    
 
    <?= $form->field($model, 'payment_date')->widget(DatePicker::classname(),[
                    'options' => ['placeholder' => '选择日期'],
                    'pluginOptions' => [
                        'format' => 'yyyy-mm-dd',
                        'autoclose'=>true,
                        'todayHighlight' => true
                    ]
                ]); ?>

    <div class="form-group text-center">
        <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

